﻿using System;
using System.Collections.Generic;
using System.Text;

namespace YiYiTong.Rocket.Helper
{
    /// <summary>
    /// json生成帮助
    /// 作者：leev
    /// 时间：2017-12-28
    /// </summary>
    public class JsonNode
    {
        private string name;

        private int valueType = 3;

        private string sValue;//0
        private JsonNode jValue;//1
        private ValueType nValue;//2
        private List<JsonNode> lValue;//3
        private List<JsonNode> aValue;//4

        /// <summary>
        /// 公开节点属性
        /// </summary>
        public List<JsonNode> JsonNodeList
        {
            get
            {
                return lValue;
            }
        }

        public static JsonNode GetNode(string value)
        {
            JsonNode n = new JsonNode();
            n.sValue = value;
            n.valueType = 0;
            return n;
        }

        public static JsonNode GetNode(JsonNode value)
        {
            JsonNode n = new JsonNode();
            n.jValue = value;
            n.valueType = 1;
            return n;
        }

        public static JsonNode GetNode(ValueType value)
        {
            JsonNode n = new JsonNode();
            n.nValue = value;
            n.valueType = 2;
            return n;
        }

        public static JsonNode GetNode(List<JsonNode> value)
        {
            JsonNode n = new JsonNode();
            n.aValue = value;
            n.valueType = 4;
            return n;
        }

        public void AddNode(string name, string value)
        {
            if (lValue == null)
            {
                lValue = new List<JsonNode>();
            }
            if (valueType == 3)
            {
                JsonNode n = new JsonNode();
                n.name = name;
                n.sValue = value;
                n.valueType = 0;
                lValue.Add(n);
            }
        }

        public void AddNode(string name, JsonNode value)
        {
            if (lValue == null)
            {
                lValue = new List<JsonNode>();
            }
            if (valueType == 3)
            {
                JsonNode n = new JsonNode();
                n.name = name;
                n.jValue = value;
                n.valueType = 1;
                lValue.Add(n);
            }
        }

        public void AddNode(string name, ValueType value)
        {
            if (lValue == null)
            {
                lValue = new List<JsonNode>();
            }
            if (valueType == 3)
            {
                JsonNode n = new JsonNode();
                n.name = name;
                n.nValue = value;
                n.valueType = 2;
                lValue.Add(n);
            }
        }

        public void AddNode(string name, List<JsonNode> value)
        {
            if (lValue == null)
            {
                lValue = new List<JsonNode>();
            }
            if (valueType == 3)
            {
                JsonNode n = new JsonNode();
                n.name = name;
                n.aValue = value;
                n.valueType = 4;
                lValue.Add(n);
            }
        }

        public override string ToString()
        {
            return ToJson();
        }

        public string ToJson()
        {
            StringBuilder strBuilder = new StringBuilder();
            if (valueType == 0)
            {
            }
            else if (valueType == 1)
            {
            }
            else if (valueType == 2)
            {
            }
            else if (valueType == 3)
            {
                extractList(lValue, strBuilder);
            }
            else if (valueType == 4)
            {
            }
            return strBuilder.ToString();
        }

        public void ToJson(StringBuilder strBuilder)
        {
            if (valueType == 0)
            {
            }
            else if (valueType == 1)
            {
            }
            else if (valueType == 2)
            {
            }
            else if (valueType == 3)
            {
                extractList(lValue, strBuilder);
            }
            else if (valueType == 4)
            {
            }
        }

        private static void extractList(List<JsonNode> value, StringBuilder strBuilder)
        {
            if (value == null || value.Count == 0)
            {
                strBuilder.Append("null");
            }
            else
            {
                strBuilder.Append("{");
                bool first = true;
                foreach (JsonNode node in value)
                {
                    if (!first)
                    {
                        strBuilder.Append(",");
                    }

                    strBuilder.Append("\"" + node.name + "\":");

                    if (node.valueType == 0)
                    {
                        strBuilder.Append("\"" + node.sValue + "\"");
                    }
                    else if (node.valueType == 1)
                    {
                        node.jValue.ToJson(strBuilder);
                    }
                    else if (node.valueType == 2)
                    {
                        strBuilder.Append(node.nValue);
                    }
                    else if (node.valueType == 3)
                    {
                        extractList(node.lValue, strBuilder);
                    }
                    else if (node.valueType == 4)
                    {
                        extractArray(node.aValue, strBuilder);
                    }

                    first = false;
                }
                strBuilder.Append("}");
            }
        }

        private static void extractArray(List<JsonNode> value, StringBuilder strBuilder)
        {
            if (value == null || value.Count == 0)
            {
                strBuilder.Append("null");
            }
            else
            {
                strBuilder.Append("[");
                bool first = true;
                foreach (JsonNode node in value)
                {
                    if (!first)
                    {
                        strBuilder.Append(",");
                    }

                    if (node.valueType == 0)
                    {
                        strBuilder.Append("\"" + node.sValue + "\"");
                    }
                    else if (node.valueType == 1)
                    {
                        node.jValue.ToJson(strBuilder);
                    }
                    else if (node.valueType == 2)
                    {
                        strBuilder.Append(node.nValue);
                    }
                    else if (node.valueType == 3)
                    {
                        extractList(node.lValue, strBuilder);
                    }
                    else if (node.valueType == 4)
                    {
                        extractArray(node.aValue, strBuilder);
                    }

                    first = false;
                }
                strBuilder.Append("]");
            }
        }

        #region debug

        public string ToJsonDebug()
        {
            StringBuilder strBuilder = new StringBuilder();
            if (valueType == 0)
            {
            }
            else if (valueType == 1)
            {
            }
            else if (valueType == 2)
            {
            }
            else if (valueType == 3)
            {
                extractListDebug(lValue, strBuilder, "");
            }
            else if (valueType == 4)
            {
            }
            return strBuilder.ToString();
        }

        public void ToJsonDebug(StringBuilder strBuilder, string tab)
        {
            if (valueType == 0)
            {
            }
            else if (valueType == 1)
            {
            }
            else if (valueType == 2)
            {
            }
            else if (valueType == 3)
            {
                extractListDebug(lValue, strBuilder, tab + "    ");
            }
            else if (valueType == 4)
            {
            }
        }

        private static void extractListDebug(List<JsonNode> value, StringBuilder strBuilder, string tab)
        {
            if (value == null || value.Count == 0)
            {
                strBuilder.Append("null");
            }
            else
            {
                strBuilder.Append(tab);
                strBuilder.Append("{\r\n");
                bool first = true;
                foreach (JsonNode node in value)
                {
                    if (!first)
                    {
                        strBuilder.Append(",\r\n");
                    }
                    strBuilder.Append(tab + "    ");

                    strBuilder.Append("\"" + node.name + "\":");

                    if (node.valueType == 0)
                    {
                        strBuilder.Append("\"" + node.sValue + "\"");
                    }
                    else if (node.valueType == 1)
                    {
                        strBuilder.Append("\r\n");
                        node.jValue.ToJsonDebug(strBuilder, tab);
                    }
                    else if (node.valueType == 2)
                    {
                        strBuilder.Append(node.nValue);
                    }
                    else if (node.valueType == 3)
                    {
                        extractListDebug(node.lValue, strBuilder, tab);
                    }
                    else if (node.valueType == 4)
                    {
                        strBuilder.Append("\r\n");
                        extractArrayDebug(node.aValue, strBuilder, tab + "    ");
                    }

                    first = false;
                }
                strBuilder.Append("\r\n" + tab + "}");
            }
        }

        private static void extractArrayDebug(List<JsonNode> value, StringBuilder strBuilder, string tab)
        {
            if (value == null || value.Count == 0)
            {
                strBuilder.Append("null");
            }
            else
            {
                strBuilder.Append(tab);
                strBuilder.Append("[\r\n");
                bool first = true;
                foreach (JsonNode node in value)
                {
                    if (!first)
                    {
                        strBuilder.Append(",\r\n");
                    }

                    if (node.valueType == 0)
                    {
                        strBuilder.Append(tab + "    ");
                        strBuilder.Append("\"" + node.sValue + "\"");
                    }
                    else if (node.valueType == 1)
                    {
                        strBuilder.Append("\r\n");
                        node.jValue.ToJsonDebug(strBuilder, tab);
                    }
                    else if (node.valueType == 2)
                    {
                        strBuilder.Append(tab + "    ");
                        strBuilder.Append(node.nValue);
                    }
                    else if (node.valueType == 3)
                    {
                        extractListDebug(node.lValue, strBuilder, tab + "    ");
                    }
                    else if (node.valueType == 4)
                    {
                        strBuilder.Append(tab + "    ");
                        strBuilder.Append("\r\n");
                        extractArrayDebug(node.aValue, strBuilder, tab + "    ");
                    }

                    first = false;
                }
                strBuilder.Append("\r\n" + tab + "]");
            }
        }

        #endregion debug
    }
}